package com.wanger.swa.convert;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanger.swc.exception.BusEx;
import com.wanger.swc.util.validation.MyVerifyUtil;
import com.wanger.swa.dto.template.req.TemplateAddReq;
import com.wanger.swa.dto.template.req.TemplateUpdateReq;
import com.wanger.swa.dto.template.resp.TemplateResp;
import com.wanger.swa.domain.po.TemplatePO;
import com.wanger.swa.dto.templateDynamicField.resp.TemplateDynamicFieldResp;
import com.wanger.swa.service.ise.TemplateDynamicFieldService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: wanger
 * @Date: 2024/9/16 17:40
 * @Description: 模板
 */
@Component
public class TemplateConvert {
    private TemplatePO baseVerify(TemplatePO po) {
        MyVerifyUtil.nullVerify(po::getPid, "pid不能为空！");
        MyVerifyUtil.nullVerify(po::getName, "模板名称不能为空！");
        MyVerifyUtil.nullVerify(po::getType, "模板类型不能为空！");
        return po;
    }

    /**
     * 验证展开字段，展开字段与别名只能同时为空或者同时不为空
     *
     * @param po 模板对象
     */
    private void expandFieldValidation(TemplatePO po) {
        BusEx bus = BusEx.create("展开字段与别名只能[同时为空]或[同时不为空]！");
        if (StrUtil.isBlank(po.getUnfoldField())) {
            if (StrUtil.isNotBlank(po.getUnfoldFieldAlias())) throw bus;
        } else {
            if (StrUtil.isBlank(po.getUnfoldFieldAlias())) throw bus;
        }
    }

    public TemplatePO toSaveEntity(TemplateAddReq addDTO) {
        TemplatePO entity = baseVerify(BeanUtil.copyProperties(addDTO, TemplatePO.class));
        expandFieldValidation(entity);
        return entity;
    }

    public TemplatePO toUpdateEntity(TemplateUpdateReq updateDTO) {
        TemplatePO po = BeanUtil.copyProperties(updateDTO, TemplatePO.class);
        MyVerifyUtil.nullVerify(po::getId, "id不能为空！");
        expandFieldValidation(po);
        return po;
    }

    public TemplateResp toDetailsDTO(TemplatePO template) {
        return toListDTO(CollUtil.toList(template))
                .stream()
                .findFirst()
                .orElse(null);
    }

    @Resource
    private TemplateDynamicFieldService templateDynamicFieldService;

    public List<TemplateResp> toListDTO(List<TemplatePO> templateList) {
        List<TemplateResp> dtos = BeanUtil.copyToList(templateList, TemplateResp.class);
        Map<String, List<TemplateDynamicFieldResp>> list = templateDynamicFieldService.getListByTemplateIds(
                dtos.stream().map(TemplatePO::getId).filter(StrUtil::isNotBlank).collect(Collectors.toSet())
        );
        dtos.forEach(dto -> dto.setDynamicFields(list.getOrDefault(dto.getId(), Collections.emptyList())));
        return dtos;
    }

    public IPage<TemplateResp> toPageDTO(IPage<TemplatePO> page) {
        return new Page<TemplateResp>()
                .setRecords(toListDTO(page.getRecords()))
                .setTotal(page.getTotal())
                .setSize(page.getSize())
                .setCurrent(page.getCurrent())
                .setPages(page.getPages());
    }
}
